<?php
/**
 * Created by PhpStorm.
 * User: mt
 * Date: 2018/12/11
 * Time: 18:06
 */

namespace App\Admin\Extensions;

use App\Dealer;
use App\Http\Resources\ExcelDealersResource;
use Encore\Admin\Grid\Exporters\AbstractExporter;
use Maatwebsite\Excel\Facades\Excel;

class ExcelExporter extends AbstractExporter
{
    public function export()
    {
        Excel::create('经销商订单信息表', function($excel) {

            $excel->sheet('Sheet1', function($sheet) {

                // 这段逻辑是从表格数据中取出需要导出的字段
                $rows = collect($this->getData());
                $sheet->rows($rows);
            });

        })->export('xls');
    }

    public function getData($toArray = true)
    {
        $header = [
            'region' => '营销总部',
            'market' => '区域',
            'company' => '公司名称',
            'username' => '账号',
            'password' => '密码',
            'name' => '负责人',
            'market_level' => '区域等级',
            'goods_1' => $GLOBALS['goods'][0]->name,
            'goods_2' => $GLOBALS['goods'][1]->name,
            'goods_3' => $GLOBALS['goods'][2]->name,
            'goods_4' => $GLOBALS['goods'][3]->name,
            'goods_5' => $GLOBALS['goods'][4]->name,
            'goods_6' => $GLOBALS['goods'][5]->name,
            'MoneyCount' => '金额合计',
        ];
        $arr =  ExcelDealersResource::collection(Dealer::where('is_fake', 0)->get());
        $list = collect($arr)->toArray();
        array_unshift($list, $header);
        return $list;
    }
}